class GroupController {
  def scaffold = true

  def show = {
    if (!params.offset) params.offset = 0
    else params.offset = Integer.parseInt(params.offset) 
    def group = Group.get(params.id)
    def groups = Group.findAllBySection(group.section)
    def topics = Topic.createCriteria().list() {
      eq("group", group)
      maxResults(30)
      firstResult(params.offset)
      order('sticky', 'desc')
      if (params.lastmod)
        order('lastmod', 'desc')
      order('commitdate', 'desc')
    }

    def total = Topic.countByGroup(group)
    //http://jira.codehaus.org/browse/GRAILS-2735

    render view: 'show', model: [groupInstance: group, groups: groups, topics: topics, total: total]
  }

  def show_lastmod = {
    params.lastmod = true
    show.call()
  }

  def rss = {
    def group = Group.get(params.id)
    def topics = Topic.findAllByGroup(group, [max: '30', sort: 'lastmod', order: 'desc'])
    //todo: criteria for non-deleted only?

    render(feedType: "rss", feedVersion: "2.0") {
      title = "${group.section.name} - ${group.title}"
      link = "http://www.linux.org.ru/"
      description = "Linux.org.ru: ${group.section.name} - ${group.title}"
      topics.each() {topic ->
        entry(topic.title) {
          link = createLink(controller: 'topic', action: 'show', id: topic.id, absolute: true)
          content(topic.message)
        }
      }
    }
  }
}
